#ifndef NP_SPIDER_H
#define NP_SPIDER_H
#include <map>
#include <config/VarDef.h>
#include <config/Structures.h>
#include <config/Rule.h>
#include <dao/DaoArticle.h>
#include <dao/DaoChapter.h>
#include <dao/DaoAttach.h>
#include <utils/XFile.h>
#include <analyser/AnalyseArticlelist.h>
#include <analyser/AnalyseArticle.h>
#include <analyser/AnalyseContents.h>
#include <analyser/AnalyseChapter.h>

using namespace ruleconfiguration;

class Spider
{
public:
  Spider();
  ~Spider();
  void work();
private:

  /**
      * 初始化
      */
  void initialize();

  /**
      * 获取小说列表
      */
  void getNovelList(AnalyseArticleList& analyseArticleList,
                    AnalyseArticle& analyseArticle,
                    AnalyseContents& analyseContents,
                    AnalyseChapter& analyseChapter);

  /**
      * 获取小说信息
      */
  void getNovel(const string& articleLink, AnalyseArticle &analyseArticle);

  /**
      * 获取目录列表
      */
  void getContents(AnalyseArticle &analyseArticle, AnalyseContents &analyseContents);

  /**
      * 获取章节信息
      */
  void getChapter(AnalyseChapter& analyseChapter, chapterInfo::ChapterInfo& chapterInfo);

  /**
    * 获取VIP章节信息
    */
  void getVipChapters(AnalyseContents& analyseContents);

  /**
      *入库小说
      */
  void storageNovel(AnalyseArticle& analyseArticle);

  /**
    * 采集小说封面
    */
  void gatherCover(const string& coverLink, const string& bookID);

  /**
      *入库章节
      */
  bool storageChapters(AnalyseContents& analyseContents, AnalyseChapter& analyseChapter);

  void gatherChapterContent(const chapterInfo::ChapterInfo& chapter, const std::string& content);

  void getAttachImage(attachInfo::AttachInfo& attach);

  void printNovelInfo(const articleInfo::ArticleInfo& article);

  void printChapter(const chapterInfo::ChapterInfo& chapter);

  void getSecondRules(std::vector<std::string> &rules);

  void completeArtcle();

  void addAnalyseContents(string key);

private:
  XFile m_file;
  chapterInfo::ChapterInfo* m_chapterList;              //存储已有卷列表

  DaoArticle m_daoArticle;
  DaoChapter m_daoChapter;
  DaoAttach m_daoAttach;

  Rule* m_firstRule;                                                      //首选站规则类
  AnalyseArticleList m_firstAnalyseArticleList;         //首选站小说列表分析类
  AnalyseArticle m_firstAnalyseArticle;                    //首选站小说分析类
  AnalyseContents m_firstAnalyseContents;            //首选站章节列表分析类
  AnalyseChapter m_firstAnalyseChapter;               //首选站章节分析类

  Rule* m_secondRule;                                                //备用站规则类
  AnalyseArticleList m_secondAnalyseArticleList;   //备用站小说列表分析类
  AnalyseArticle m_secondAnalyseArticle;              //备用站小说分析类
  AnalyseContents m_secondAnalyseContents;      //备用站章节列表分析类
  AnalyseChapter m_secondAnalyseChapter;         //备用站章节分析类

  std::string m_htmlString;
  compar::typeRank m_rank;
  int m_hasVips;
  typedef struct lastInfo
  {
    std::string articleid;
    std::string lastchapterid;
    std::string lastchapter;
    std::string lastvolumeid;
    std::string lastvolume;
    int chapters;
  }LastInfo;

  LastInfo m_lastinfo ;

  typedef std::map<std::string,chapterInfo::ChapterInfo*> typeChapters;

  typeChapters m_chapterResultList;

  typedef std::map<std::string,AnalyseContents*> typeContents;
  typeContents m_contentsList;          //存储所有备用站的章节分析内容
  int m_repeat;                         //重复采集次数

};

#endif // NP_SPIDER_H
